package day23;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * 
 * IPDemo.java
 * 
 * @author wangbiao<br/>
 *         email:785497344@qq.com
 * @description 获取IP地址
 * @since 1.0.0
 * @date 2013-7-2下午11:55:40
 * 
 */
public class IPDemo {

	public static void main(String[] args) {
		getLocalHost();
		getIpByName();
	}

	/**
	 * @author wangbiao<br/>
	 *         email:785497344@qq.com
	 * @description 获取远程主机的ip地址
	 * @since 1.0.0
	 * @date 2013-7-2下午11:55:51
	 * 
	 */
	public static void getIpByName() {
		try {
			InetAddress i = InetAddress.getByName("localhost");
			InetAddress[] all = InetAddress.getAllByName("www.baidu.com");
			for (int j = 0, k = all.length; j < k; j++) {
				System.out.println("baidu=" + all[j].getHostAddress());
			}
			System.out.println(i.getHostAddress());
			System.out.println(i.getHostName());
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @author wangbiao<br/>
	 *         email:785497344@qq.com
	 * @description 获取本地的ip地址和主机名称
	 * @since 1.0.0
	 * @date 2013-7-2下午11:49:27
	 * 
	 */
	public static void getLocalHost() {
		try {
			InetAddress i = InetAddress.getLocalHost();
			String address = i.getHostAddress();
			String name = i.getHostName();
			System.out.println("toString" + i.toString());
			System.out.println("address=" + address);
			System.out.println("name=" + name);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}

	}
}
